//
// Created by 17616 on 2021/4/22.
//
#include"stm32_hal.h"
#include"CLI.h"
extern I2C_HandleTypeDef hi2c1;
extern SPI_HandleTypeDef hspi1;
extern SPI_HandleTypeDef hspi2;
extern UART_HandleTypeDef huart1;
extern DMA_HandleTypeDef hdma_usart1_rx;
extern "C" int _write(int fd, char* ptr, int len)
{
    while (HAL_UART_Transmit(&huart1, (uint8_t*)ptr, len, 0xfffff) != HAL_OK) {}
    return len;
}
uint8_t uart1_rx_buffer[1024];
shell::CLI cli((shell::Write)_write, NULL, 1024, "c_inv_imu:");
extern "C" int cpp_main(){
    __HAL_UART_CLEAR_IDLEFLAG(&huart1);//清除标志位
    __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);//使能idle中断
    HAL_UART_Receive_DMA(&huart1, uart1_rx_buffer, sizeof(uart1_rx_buffer));//打开DMA接收，数据存入rx_buffer数组中。
    cli.ShowTitle();
    cli.ShowHead();
    while(1){
        cli.Process();
    }
    return 0;
}


extern "C"
void USART1_IRQHandler(void)
{
    if ((__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE) != RESET))//idle标志被置位
    {
        __HAL_UART_CLEAR_IDLEFLAG(&huart1);//清除标志位
        HAL_UART_DMAStop(&huart1);
        cli.Put((char*)uart1_rx_buffer, sizeof(uart1_rx_buffer) - __HAL_DMA_GET_COUNTER(&hdma_usart1_rx));
        HAL_UART_Receive_DMA(&huart1, uart1_rx_buffer, sizeof(uart1_rx_buffer));//打开DMA接收，数据存入rx_buffer数组中。//开启下一次传输
    }
//    HAL_UART_IRQHandler(&huart1);
}